package ro.myApp.esb.transformers;

import javax.ejb.TransactionAttributeType;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageAwareTransformer;

import ro.myApp.commons.dto.BaseDtoMessage;
import ro.myApp.esb.messages.EsbMessage;
import ro.myApp.esb.messages.EsbMessageTransactionNew;
import ro.myApp.esb.messages.EsbMessageTransactionNotSupported;
import ro.myApp.esb.messages.EsbMessageTransactionRequired;

public class BaseDtoMessageToEsbMessageTransformer extends
		AbstractMessageAwareTransformer {

	@Override
	public Object transform(MuleMessage msg, String arg1)
			throws TransformerException {
		BaseDtoMessage wrappedMessage = (BaseDtoMessage)msg.getOrginalPayload();
		TransactionAttributeType transactionType = (TransactionAttributeType)msg.getProperty("TransactionType");	
		
		EsbMessage j = null;
		
		if(transactionType == null)
			transactionType = TransactionAttributeType.REQUIRES_NEW;
		
		switch (transactionType) {
		
			case NOT_SUPPORTED:
				j = new EsbMessageTransactionNotSupported(wrappedMessage);
				break;
			
			case REQUIRED:
				j = new EsbMessageTransactionRequired(wrappedMessage);
				break;
			
			default:
				j = new EsbMessageTransactionNew(wrappedMessage);
				
		}
		j.setUserName(getUserName());
		return j;
	}

	protected String getUserName() {
		// TODO Auto-generated method stub
		return null;
	}

}
